// Copyright (C) 2010  Joan Puigcerver Perez, <joapuipe@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#ifndef __XMLCONFIG_HPP__
#define __XMLCONFIG_HPP__

#include <cc++/common.h>

#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/sax2/Attributes.hpp>

#include <string>

#ifdef XERCES_CPP_NAMESPACE_USE
XERCES_CPP_NAMESPACE_USE
#endif

class SAXConfigHandler: public DefaultHandler {
private:
  XMLCh *_kelemConnection, *_kattrDB, *_kattrHost, *_kattrUser, *_kattrPass;
  std::string host, db, user, pass;
public:  
  /** This function is executed by the parser when the document starts. */
  void startDocument ()
  {
    _kelemConnection = XMLString::transcode("connection");
    _kattrDB    = XMLString::transcode("db");
    _kattrHost  = XMLString::transcode("host"); 
    _kattrUser  = XMLString::transcode("user");
    __kattrPass = XMLString::transcode("pass");
  }

  /** This function is executed by the parser when the document ends. */
  void endDocument ()
  {
    XMLString::release(&_kelemConnection);
    XMLString::release(&_kattrHost);
    XMLString::release(&_kattrDB);
    XMLString::release(&_kattrUser);
    XMLString::release(&_kattrPass);
  }

  /** 
   * Receive notification of the start of an element.
   *
   * @param uri The URI of the associated namespace for this element
   * @param localname The local part of the element name
   * @param qname The QName of this element
   * @param attrs The specified or defaulted attributes. 
   */
  void startElement ( const XMLCh* const uri,
		      const XMLCh* const localname,
		      const XMLCh* const qname,
		      const Attributes&  attrs )
  {
    if ( XMLString::equal(uri, _kelemConnection) ) {
      char * _host = XMLString::transcode( attrs.getValue(_kattrHost) );
      char * _db   = XMLString::transcode( attrs.getValue(_kattrDB) );
      char * _user = XMLString::transcode( attrs.getValue(_kattrUser) );
      char * _pass = XMLString::transcode( attrs.getValue(_kattrPass) );
      
      db = _db;
      host = _host;
      user = _user;
      pass = _pass;

      XMLString::release(&_host);
      XMLString::release(&_db);
      XMLString::release(&_user);
      XMLString::release(&_pass);
    }
  }

  /** 
   * Receive notification of the end of an element.
   *
   * @param uri The URI of the associated namespace for this element
   * @param localname The local part of the element name
   * @param qname The QName of this element
   */
  void endElement( const XMLCh* const uri, 
		   const XMLCh* const localname,
		   const XMLCh* const qname )
  {
  }
  
  /** 
   * Receive notification of character data inside an element.
   *
   * @param chars The characters.
   * @param length The number of caracters to use from the character array.
   */
  void characters( const XMLCh* const chars,
		   const XMLSize_t length )
  {
  }

  /**
   * Report a fatal XML parsing error. 
   * 
   * @param exception The error information encoded as an exception.
   */
  void fatalError( const SAXParseException& exception )
  {
  }

  /**
   * Report a XML parsing error, usually it happens when the request does not follow the schema.
   * 
   * @param exception The error information encoded as an exception.
   */  
  void error(const SAXParseException &exception)
  {
  }

};

class XMLConfig {
};

#endif
